package hongcaosp.app.android.user.settings.invite;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;

import hongcaosp.app.android.R;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.BaseResponse;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.impl.PrizeModelImpl;
import hongcaosp.app.android.user.settings.SettingsActivity;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;

/**
 * 填写邀请码
 */
public class WriteCodeActivity extends BaseActivity {

    public static void launcher(Context context) {
        context.startActivity(new Intent(context,WriteCodeActivity.class));
    }

    private EditText et_code;
    private TextView tv_invite;

    @Override
    protected void initData(Intent intent) {


    }

    @Override
    protected void initView() {
        et_code = findViewById(R.id.et_code);
        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = et_code.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    ToastManager.getInstance().showToast("请输入邀请码");
                    return;
                }
                new PrizeModelImpl().invitationCode(UserToken.getDefault().getToken(), code, new AbsCallback<BaseResponse>() {
                    @Override
                    public BaseResponse convertResponse(okhttp3.Response response) throws Throwable {
                        try {
                            BaseResponse baseResponse = JSON.parseObject(response.body().string(), BaseResponse.class);
                            return baseResponse;
                        } catch (Throwable e) {
                            throw e;
                        }
                    }

                    @Override
                    public void onSuccess(Response<BaseResponse> response) {
                        if (response == null || response.code() != 200 || response.body() == null) {
                            ToastManager.getInstance().showToast("提交失败");
                            return;
                        }
                        if (response.body().getCode() == 0) {
                            new DialogSuccess(WriteCodeActivity.this, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    UserToken.getDefault().getUser().setInvitationUser("-1");
                                    UserToken.getDefault().save();

                                    InviteActivity.launcher(WriteCodeActivity.this);
                                    finish();
                                }
                            }).show();
                            return;
                        }
                        ToastManager.getInstance().showToast("" + response.body().getMessage());
                    }
                });
            }
        });
        tv_invite = findViewById(R.id.tv_invite);
        tv_invite.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv_invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InviteActivity.launcher(WriteCodeActivity.this);
            }
        });
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_write_code;
    }
}
